package com.hospicpc.demos.controller;


import com.hospicpc.demos.commom.Result;
import com.hospicpc.demos.entity.Articles;
import com.hospicpc.demos.entity.CollectArticles;
import com.hospicpc.demos.entity.LikeArticles;
import com.hospicpc.demos.service.Impl.ArticlesServiceImpl;
import com.hospicpc.demos.service.Impl.CollectArticlesServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/collectArticle")
public class CollectArticleController {
    @Autowired
    CollectArticlesServiceImpl collectArticlesService;
    @Autowired
    ArticlesServiceImpl articlesService;


    /**
     * 获取收藏文章列表
     * @param userId
     * @return
     */
    @GetMapping("/getList")
    public Result getList(@RequestParam("userId") Long userId) {

        List<Articles> collectArticleList = articlesService.getCollectArticleList(userId);

        return  Result.success(collectArticleList);
    }

    /**
     * 关注文章
     * @param collectArticles
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CollectArticles collectArticles) {
        //获取数据
        //点赞的文章id
        Long  toArticleId= collectArticles.getToArticleId();
        //用户id
        Long userId = collectArticles.getUserId();
        //文章类型,0代表ai文章，1代表普通文章
        Integer articleType = collectArticles.getArticleType();

        //判断数据是否存在
        CollectArticles one = collectArticlesService.lambdaQuery().eq(CollectArticles::getUserId, userId)
                .eq(CollectArticles::getToArticleId, toArticleId)
                .eq(CollectArticles::getArticleType, articleType).one();

        boolean res;

        //存在,改变状态
        if(one != null) {
            res = collectArticlesService.lambdaUpdate().eq(CollectArticles::getUserId, userId)
                    .eq(CollectArticles::getToArticleId, toArticleId)
                    .eq(CollectArticles::getArticleType, articleType)
                    .setSql("status = 1")
                    .update();
        } else {
            //不存在,新插入数据
            res = collectArticlesService.save(collectArticles);
        }

        if(res) return Result.success("点赞成功");
        else  return Result.error("服务异常");

    }

    /**
     * 取消关注文章
     * @param likeArticles
     * @return
     */
    @PostMapping("/del")
    public Result del(@RequestBody LikeArticles likeArticles) {
        //获取数据
        //点赞的文章id
        Long toArticleId = likeArticles.getToArticleId();
        //用户id
        Long userId = likeArticles.getUserId();
        //文章类型,0代表ai文章，1代表普通文章
        Integer articleType = likeArticles.getArticleType();

        boolean res = collectArticlesService.lambdaUpdate().eq(CollectArticles::getUserId, userId)
                .eq(CollectArticles::getToArticleId, toArticleId)
                .eq(CollectArticles::getArticleType, articleType)
                .setSql("status = 0")
                .update();

        if (res) return Result.success("取消点赞成功");
        else return Result.error("服务异常");
    }
}
